package com.xiaoyu.lanling.feature.prize

import `in`.srain.cube.request.JsonData
import android.os.Bundle
import android.text.SpannableString
import android.text.style.RelativeSizeSpan
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.DialogFragment
import androidx.fragment.app.FragmentManager
import com.xiaoyu.base.utils.extensions.setOnClickDebounceListener
import com.xiaoyu.lanling.R
import com.xiaoyu.lanling.activity.base.BaseDialogFragment
import com.xiaoyu.lanling.common.Constant
import com.xiaoyu.lanling.feature.prize.PrizeData.Companion.KEY_PRIZE_COIN
import com.xiaoyu.lanling.feature.user.data.UserInfoData
import kotlinx.android.synthetic.main.dialog_prize.*

class PrizeDialogFragment : BaseDialogFragment() {

    companion object {
        val TAG: String = PrizeDialogFragment::class.java.simpleName

        fun showIfNeed(fragmentManager: FragmentManager, key: String, jsonData: JsonData) {
            val params = Bundle()
            params.putString(Constant.IntentKey.KEY_PRIZE_KEY, key)
            params.putString(Constant.IntentKey.KEY_PRIZE_JSON_DATA, jsonData.toString())

            val fragment = PrizeDialogFragment()
            fragment.arguments = params
            fragment.show(fragmentManager, TAG)
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setStyle(DialogFragment.STYLE_NO_TITLE, R.style.DialogFullScreenDimStyle)
    }

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        return layoutInflater.inflate(R.layout.dialog_prize, container, false)
    }

    override fun onViewCreatedSafelyAfterAppFinishInit(view: View, savedInstanceState: Bundle?) {
        super.onViewCreatedSafelyAfterAppFinishInit(view, savedInstanceState)
        initView()
        initBind()
    }

    private fun initView() {
        val key = arguments?.getString(Constant.IntentKey.KEY_PRIZE_KEY) ?: return
        val jsonString = arguments?.getString(Constant.IntentKey.KEY_PRIZE_JSON_DATA) ?: return
        val jsonData = JsonData.create(jsonString)
        val isCoin = key == KEY_PRIZE_COIN

        coin_desc.setCompoundDrawablesRelativeWithIntrinsicBounds(if (isCoin) R.drawable.icon_coin_big else 0, 0, 0, 0)
        if (isCoin) {
            coin_desc.text = getString(R.string.prize_coin_desc, jsonData.optString("amount"))
        } else {
            val spannableString = SpannableString(getString(R.string.prize_cash_desc, jsonData.optString("amount")))
            spannableString.setSpan(RelativeSizeSpan(1.5F), 0, spannableString.length - 1, SpannableString.SPAN_INCLUSIVE_INCLUSIVE)
            spannableString.setSpan(RelativeSizeSpan(0.5F), spannableString.length - 1, spannableString.length, SpannableString.SPAN_EXCLUSIVE_INCLUSIVE)
            coin_desc.text = spannableString
        }
        title.text = jsonData.optString("text")
        desc.text = jsonData.optString("tip")

        updateDoNotRemind(jsonData.optString("type"))
    }

    private fun updateDoNotRemind(prizeType: String) {
        val visibility = if (prizeType == PrizeType.ACCOST) View.VISIBLE else View.GONE
        do_not_remind_desc.visibility = visibility
        do_not_remind_check_view.visibility = visibility
    }

    private fun initBind() {
        close.setOnClickDebounceListener { dismissAllowingStateLoss() }
        background.setOnClickDebounceListener {
            background.isEnabled = false
            showData()
        }
        do_not_remind_check_view.setOnClickDebounceListener {
            UserInfoData.setUserSetting(Any(), do_not_remind_check_view.isSelected)
            do_not_remind_check_view.isSelected = !do_not_remind_check_view.isSelected
        }
        do_not_remind_desc.setOnClickListener { do_not_remind_check_view.performClick() }
    }

    private fun showData() {
        coin_desc.visibility = View.VISIBLE
        desc.visibility = View.VISIBLE
        do_not_remind_check_view.visibility = View.GONE
        do_not_remind_desc.visibility = View.GONE
    }
}